/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "ChrModelTreeWidget.h"

#include <QtGui/QLayout>
#include <QtGui/QMouseEvent>
#include <QtGui/QApplication>
#include <QtGui/QPushButton>
#include <QtGui/QFileDialog>

#include "ChrModelManager.h"
#include "GEngine.h"
#include "GExecutor.h"

namespace GBuilder
{
	ChrModelTree::ChrModelTree(QWidget *parent)
	{
		this->setHeaderLabel(tr("Character"));
		init();
	}

	void ChrModelTree::init()
	{
		int cnt=GNgChrModelMgr.getRaceCount();
		
		for(int i=0;i<cnt;i++){
			std::string raceName=GNgChrModelMgr.getRaceName(i);

			QTreeWidgetItem* item=new QTreeWidgetItem(this,QStringList(QString::fromStdString(raceName)));
			this->addTopLevelItem(item);

			QTreeWidgetItem* maleItem=new QTreeWidgetItem(QStringList(tr("male")));
			QTreeWidgetItem* femaleItem=new QTreeWidgetItem(QStringList(tr("female")));
			item->addChild(maleItem);
			item->addChild(femaleItem);

			int modelCnt=GNgChrModelMgr.getRaceMaleModelCount(i);
			for(int j=0;j<modelCnt;j++){
				std::string modelName=GNgChrModelMgr.getRaceMaleModelName(i,j);
				QTreeWidgetItem* modelItem=new QTreeWidgetItem(QStringList(QString::fromStdString(modelName)));

				maleItem->addChild(modelItem);
			}

			modelCnt=GNgChrModelMgr.getRaceFemaleModelCount(i);
			for(int j=0;j<modelCnt;j++){
				std::string modelName=GNgChrModelMgr.getRaceFemaleModelName(i,j);
				QTreeWidgetItem* modelItem=new QTreeWidgetItem(QStringList(QString::fromStdString(modelName)));
				femaleItem->addChild(modelItem);
			}

		}

	}

	void ChrModelTree::mousePressEvent(QMouseEvent *evt)
	{
		QTreeWidget::mousePressEvent(evt);
		if (evt->button() == Qt::LeftButton){
			_dragStartPosition = evt->pos();
		}
	}

	void ChrModelTree::mouseMoveEvent(QMouseEvent *evt)
	{

		if (!(evt->buttons() & Qt::LeftButton)){
			return;
		}
		
		if ((evt->pos() - _dragStartPosition).manhattanLength()< QApplication::startDragDistance()){
			return;
		}
		
		QTreeWidgetItem *item=this->currentItem();
		if(item->childCount()>0){//only drag leaf item(model file path)
			return;
		}

		QString path="character/"+getItemPath(item);

		if(!GNgChrModelMgr.modelFileExist(path.toStdString())){
			return;
		}
		
		//add leaf m2 model into scene;
		GNgine::GDatagram *datagram=GNgineExec::genAddChrModel(path.toStdString());

		if(datagram==0){
			return;
		}

		QByteArray bytes=QByteArray(datagram->data(),datagram->length());
		QDrag *drag = new QDrag(this);
		QMimeData *mimeData = new QMimeData;
		mimeData->setData(QString::fromStdString(GNgine::GEngine::COMMAND_DATA_TYPE),bytes);
		drag->setMimeData(mimeData);
		Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);


	}

	QString ChrModelTree::getItemPath(QTreeWidgetItem* item)
	{
		if(!item){
			return QString::null;
		}

		QTreeWidgetItem* parent=item->parent();
		std::vector<QTreeWidgetItem*> list;

		while(parent!=0){
			list.push_back(parent);
			parent=parent->parent();
		}

		QString path;
		int size=list.size();
		if(size==0){
			path=item->text(0);
		}else{
			for(int i=size-1;i>=0;i--){
				path+=list[i]->text(0);
				path+="/";
			}
			path+=item->text(0);
		}
		return path;
	}


	ChrModelTreeWidget::ChrModelTreeWidget(QWidget *parent)
	{
		_treeWidget = new ChrModelTree(this);

		QVBoxLayout *boxlayout = new QVBoxLayout(this);
		boxlayout->setMargin(0);
		boxlayout->addWidget(_treeWidget);
		boxlayout->setStretch(0,0);
		boxlayout->setStretch(1,1);
	}

	ChrModelTreeWidget::~ChrModelTreeWidget()
	{
	}

}
